/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.messaging.Message;
import org.openide.util.Lookup;

/**
 * Class which creates a panel in mail inbox style. The class requires messageSender, messageSubject and messageDate. The messageSender will be placed on the top-left. 
 * The messageDate will be placed on the top-right and the messageSubject will be placed on the left-bottom.
 * @author Merijn Zengers
 */
public class InboxRow extends JTextField implements FocusListener{
    private JLabel senderLabel;
    private JLabel subjectLabel;
    private JLabel sendDateLabel;
    
    private String sender;
    private String messageSubject;
    private Date messageSendDate;
    
    private Message message;
    
    private boolean selected = false;
    
    private Border border;
    private final Color DEFAULT_COLOR = Color.WHITE;
    private final Color BORDER_COLOR = Color.LIGHT_GRAY;
    private final Color SELECTED_COLOR = Color.LIGHT_GRAY;
    
    private final int MINIMUM_WIDTH = 195;
    private final int MINIMUM_HIEGHT = this.getFont().getSize() * 4;
    
    /**
     * Creates a new message row
     * @param messageSender the sender of the message.
     * @param messageSubject the subject of the message
     * @param messageSendDate the date of the message
     */
    public InboxRow(Message message) {
        this.message = message;
        
        this.sender = message.getOrigin();
        this.messageSubject = message.getTitle();
        this.messageSendDate = message.getDateReceived();
        
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
        this.setLayout(new MigLayout("wrap 2, insets 5"));
        senderLabel = new JLabel(this.sender);
        subjectLabel = new JLabel(this.messageSubject);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        sendDateLabel = new JLabel(messageSendDate!=null?sdf.format(messageSendDate):"");
        this.setBackground(DEFAULT_COLOR);
        border = BorderFactory.createLineBorder(BORDER_COLOR, 1);;
        this.setBorder(border);
        add(senderLabel, "align left");
        add(sendDateLabel, "gapleft push");
        add(subjectLabel,"align left");
        
        
        
        Dimension dimension = new Dimension(MINIMUM_WIDTH, MINIMUM_HIEGHT);
        this.setPreferredSize(dimension);
        this.setEditable(false);
        this.setName(sender);
        this.addFocusListener(this);
    }
    
    /**
     * returns if the message is selected
     * @return message selected
     */
    boolean isSelected(){
        return selected;
    }
    
    /**
     * Set the message selected
     * @param selected 
     */
    private void setSelected(boolean selected){
        this.selected = selected;
        this.setBackground(SELECTED_COLOR);
    }
    
    Message getMessage(){
        return this.message;
    } 

    @Override
    public void focusGained(FocusEvent e) {
        setSelected(true);
        this.setBackground(SELECTED_COLOR);
    }

    @Override
    public void focusLost(FocusEvent e) {
        setSelected(false);
        this.setBackground(DEFAULT_COLOR);
    }

}
